package q724_pivotIndex;

import java.util.Arrays;

public class Solution_2 {
    /*
    数组所有元素之和为total
    假设一个元素左侧元素和为sum 则其右侧元素和为 total - nums[i] - sum
    左右相等即为 2 * sum + nums[i] == total
     */
    public int pivotIndex(int[] nums) {
        int total = Arrays.stream(nums).sum();
        int sum = 0;
        for (int i = 0; i < nums.length; ++i) {
            if (2 * sum + nums[i] == total) {
                return i;
            }
            sum += nums[i];
        }
        return -1;
    }

}
